package com.belazy.library.util.compute;

import com.belazy.library.util.constant.StringConstant;
import java.io.*;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description <p> String工具类</p>
 * @Author chupeng.tang
 * @CreateTime 2023/5/19 17:29
 * @Version 1.0
 */
public class StringUtil {
    private static String DICTIONARY_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-?$%#!^";
    private static String DEFAULT_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /**
     * <p>随机生成32位字符串</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:38
     * @return {@link String}
     **/
    public static String getRandomKey32() {
        StringBuilder sb = new StringBuilder (32);
        SecureRandom random = new SecureRandom ();
        int len = 32;
        for (int i = 0; i < len; i++) {
            sb.append (DICTIONARY_CHAR.charAt (random.nextInt (DICTIONARY_CHAR.length ())));
        }
        return sb.toString ();
    }
    /**
     * <p>随机生成指定长度的字符串(不带特殊字符"+-?$%#!^")</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:39
     * @param: length
     * @return {@link String}
     **/
    public static String getRandomKey(int length) {
        StringBuilder sb = new StringBuilder (length);
        SecureRandom random = new SecureRandom ();
        for (int i = 0; i < length; i++) {
            sb.append (DEFAULT_CHAR.charAt (random.nextInt (DEFAULT_CHAR.length ())));
        }
        return sb.toString ();
    }
    /**
     * <p>判断字符是否空</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:41
     * @param: str
     * @return {@link boolean} 空返true
     **/
    public static boolean isEmpty(String str) {
        return (str == null) || ("".equals (str.trim ()));
    }
    /**
     * <p>str 转换编码 iso8859-1 -> UTF-8</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:45
     * @param: str
     * @return {@link String}
     **/
    public static String parseEncoding(String str) {
        String string = str;
        try {
            string = new String (str.getBytes ("iso8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace ();
        }
        return string;
    }
    /**
     * <p>获取yyyyMMddHHmmss 时间戳</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:45
     * @return {@link String}
     **/
    public static String getTimestamp() {
        SimpleDateFormat fmt = new SimpleDateFormat ("yyyyMMddHHmmss");
        return fmt.format (new Date ());
    }
    /**
     * <p>指定长度，随机生成数字</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:45
     * @param: size
     * @return {@link String}
     **/
    public static String getRandomNum(int size) {
        String random = (int) (Math.random () * Math.pow (10, size)) + "";
        int length = random.length ();
        if (length < size) {
            random = "0" + random;
            length++;
        }
        return random;
    }
    /**
     * <p>读取流为符串</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:34
     * @param: in
     * @return {@link String}
     **/
    public static String getFromInputSream(InputStream in) throws IOException {
        return getFromInputSream (in, "UTF-8");
    }
    /**
     * <p>读取流为符串</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:34
     * @param: in
     * @param: encoding
     * @return {@link String}
     **/
    public static String getFromInputSream(InputStream in, String encoding) throws IOException {
        BufferedReader reader = new BufferedReader (new InputStreamReader (in, encoding));
        StringBuffer buff = new StringBuffer ();
        String line = null;
        while ((line = reader.readLine ()) != null) {
            buff.append (line).append ("\n");
        }
        boolean flag = buff.toString ().endsWith ("\n");
        if (flag) {
            buff.deleteCharAt (buff.length () - 1);
        }
        return buff.toString ();

    }
    /**
     * <p>分割带逗号字符为集合</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:32
     * @param: str
     * @return {@link List< String>}
     **/
    public static List<String> toStringList(String str) {
        List<String> idList = new ArrayList ();
        if (!isEmpty (str)) {
            String[] idsArray = str.split (",");
            for (int i = 0; i < idsArray.length; i++) {
                idList.add (idsArray[i]);
            }
        }
        return idList;
    }
    /**
     * <p>转换为字节数组</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:33
     * @param: str
     * @return {@link byte[]}
     **/
    public static byte[] getBytes(String str) {
        if (str != null) {
            try {
                return str.getBytes (StringConstant.CHARSET_UTF_8);
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        } else {
            return null;
        }
    }
    /**
     * <p>转换为字节数组</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:33
     * @param: bytes
     * @return {@link String}
     **/
    public static String toString(byte[] bytes) {
        try {
            return new String (bytes, StringConstant.CHARSET_UTF_8);
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
    /**
     * <p>序列化对象</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:47
     * @param: object
     * @return {@link byte[]}
     **/
    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            if (object != null) {
                baos = new ByteArrayOutputStream ();
                oos = new ObjectOutputStream (baos);
                oos.writeObject (object);
                return baos.toByteArray ();
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }
    /**
     * <p>反序列化对象</p>
     * @auhor chupeng.tang
     * @datetime 2023/5/20 11:47
     * @param: bytes
     * @return {@link Object}
     **/
    public static Object unserialize(byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            if (bytes != null && bytes.length > 0) {
                bais = new ByteArrayInputStream (bytes);
                ObjectInputStream ois = new ObjectInputStream (bais);
                return ois.readObject ();
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }
}